<?
	
	if(isset($_GET['cid'])){$cid = intval($_GET['cid']);}else{$cid=0;}
	
	if(isset($_GET['ttid']) && ($_GET['ttid'] != "") ){$ttid = intval($_GET['ttid']);}
	
	$rs = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_id = '".$cid."' AND c_hienthi = 1");
	if($db->num_rows($rs) != 0){
		$row = $db->fetch_array($rs);
			
		if($row[c_id_cha] == 0){
			$rs2 = $db->query("SELECT * FROM ".$prefix."loaisanpham WHERE c_id_cha = '".$cid."' AND c_hienthi = 1");
			$listId = "";
			$s = 0;
			while($row2 = $db->fetch_array($rs2)){
				$s++;
				if($s != $db->num_rows($rs2)){
					$listId .= $row2[c_id] . ",";
				}else{
					$listId .= $row2[c_id];
				}
			}
			
			$sql = "SELECT * FROM ".$prefix."sanpham WHERE t_loaisanpham IN(".$listId.")";
			
		}else{
			$sql = "SELECT * FROM ".$prefix."sanpham WHERE t_loaisanpham = '".$cid."'";
		}
		
	}else{reHome($urlHome);}
	
	/////////TIM KIEM THEO TINH THANH ///////////
	if(isset($ttid) && ($ttid != "") ){
		$sql .= " AND t_tinhthanh = '".$ttid."'";
	}
	/////TIM KIEM THEO GIA
	if(isset($_GET['act']) && ($_GET['act'] == 'theogia')){
		if(isset($_GET['fgid']) && ($_GET['fgid'] != "") ){$fgid = $_GET['fgid'];}else{reHome($urlHome);}
		if(isset($_GET['tgid']) && ($_GET['tgid'] != "") ){$tgid = $_GET['tgid'];}else{reHome($urlHome);}
		//
		if($fgid == 0){
			$sql .= " AND t_giaban >= 0 AND t_giaban <= ".$tgid."";
		}else if($tgid == 0){
			$sql .= " AND t_giaban >= ".$tgid."";
		}else{
			$sql .= " AND t_giaban >= ".$fgid." AND t_giaban <= ".$tgid."";
		}
	}
	
	////////////TIM KIEM THEO TU KHOA//////////////////////
	if(isset($_GET['tukhoa']) && ($_GET['tukhoa'] != "")){
		$tukhoa = $_GET['tukhoa'];
		$sql .= " AND (t_tieude like '%".$tukhoa."%' OR t_mota like '%".$tukhoa."%' OR t_chitiet like '%".$tukhoa."%')";
	}
	/////////////////////
	if(isset($_GET['sid'])){$sid = intval($_GET['sid']);}
	
	if(isset($sid) && ($sid != "")){
		if($sid == 0){
			$sql = $sql;
		}else if($sid == 1){
			$sql .= " ORDER BY t_id DESC";
		}else if($sid == 2){
			$sql .= " ORDER BY t_id ASC";
		}else if($sid == 3){
			$sql .= " ORDER BY t_giaban ASC";
		}else{
			$sql .= " ORDER BY t_giaban DESC";
		}
	}
	//Paging
	
	$pp=10; 
	$parr=array();
	$parr=paging($sql,$pp);
	$page=$parr[0];
	$limitvalue=$parr[1];
	$numofpages=$parr[2];
	$sql.=" LIMIT $limitvalue, $pp";
	//
	$rs = $db->query($sql);
	
	
?>
<script language="javascript">
	function funSort(id,cid,p,tukhoa,act,fgid,tgid,ttid){
		window.location = '<?=$root?>/?mod=chungloai&cid=' + cid + '&sid=' + id + '&p=' + p + '&tukhoa=' + tukhoa + '&act=' + act +'&fgid=' + fgid + '&tgid=' + tgid + '&ttid=' + ttid;
		
	}
</script>
<div id="filtersDescription">
        <ul id="descriptionBar" style="margin:0px;">
            <li style="float:left;margin:0px;line-height:20px;">Tìm thấy <b class="highline"><?=$db->num_rows($rs)?></b> sản phẩm</li>

            <li style="float:right;margin:0px;line-height:20px;" id="menuContainer">
              Sắp xếp theo:
			  <select name="sort" style="width:120px;" onchange="funSort(this.value,'<?=$cid?>','<?=$p?>','<?=$tukhoa?>','<?=$act?>','<?=$fgid?>','<?=$tgid?>','<?=$ttid?>');">
			  	<option value="0" <? if($sid == 0){?> selected="selected" <? }?>>-- Tất cả --</option>
				<option value="1" <? if($sid == 1){?> selected="selected" <? }?>>Mới dần</option>
				<option value="2" <? if($sid == 2){?> selected="selected" <? }?>>Cũ dần</option>
				<option value="3" <? if($sid == 3){?> selected="selected" <? }?>>Giá tăng dần</option>
				<option value="4" <? if($sid == 4){?> selected="selected" <? }?>>Giá giảm dần</option>
			  </select>
			  </li>                       
        </ul>
        <br class="clearBoth" style="font-size:0pt;"/> 
        <p></p>  
    </div>   
</div> 

     
                         
            
<!-- Product List { -->
<div id="product-list">
    <div id="product-list-header">       
        <div class="product-col product-col-name">
            <p>Tên sản phẩm</p>

        </div>
        <div class="product-col product-col-price">
            <p>Giá</p>
        </div>
        <div class="product-col product-col-region">
            <p>Phạm vi bán</p>
        </div>
    </div>

    <div style="clear:both;height:4px;font-size:0pt;"></div>
    <div id="product-list-body">
        <div class="corner-round-05-body2"> 
            
                <table width="100%" cellpadding="0" cellspacing="0" id="products-items">

                <?
					if($db->num_rows($rs) != 0){
					$i = 0;
					while($row = $db->fetch_array($rs)){
					$i++;
					$t_id = $row[t_id];
					$t_tieude = stripslashes($row[t_tieude]);
					$t_hinhanh = $PathFile . stripslashes($row[t_hinhanh]);
					$t_mota = stripslashes($row[t_mota]);
					if($row[t_giaban] == 0){
						$t_giaban = "Call";
					}else{
						$t_giaban = showprice(stripslashes($row[t_giaban])) ." ". loadLoaiTien($row[t_loaitien]);
					}
					$t_nguoidang = $row[t_nguoidang];
					$t_tinhthanh = layTinhThanh($row[t_tinhthanh]);
					$t_xemgannhat = $row[t_xemgannhat];
					$t_khuyenmai = $row[t_khuyenmai];
					$t_sapramat = $row[t_sapramat];
					$solanxem = $row[t_solanxem];
					$comment = loadSoComment($t_id);
					
				?>
					<!-- SAN PHAM THU : -->
                    <tr>
                        <td class="col-picture">
                            <p> 
                                <a title="<?=$t_tieude?>" href="<?=$root?>/chi-tiet-san-pham/<?=$t_id?>/<?=$myString->vietdecode($t_tieude)?>" class="tips product-name">
                                    <img style="vertical-align:middle" alt="<?=$t_tieude?>" src="<?=$root?>/<?=$t_hinhanh?>" onerror="this.src='<?=$root?>/themes/images/noimg.jpg'"; width="100" height="100" border="0"/>
                                </a>
                            </p> 
                        </td>
                        <td class="col-name">
                            <h1>

                                <a href="<?=$root?>/chi-tiet-san-pham/<?=$t_id?>/<?=$myString->vietdecode($t_tieude)?>" class="product-name" title="<?=$t_tieude?>"><?=$t_tieude?></a>
								<? if($t_khuyenmai == 1){ ?>
								<blink><font color="#FF0000" style="font-size:11px; font-weight:bold;">Khuyến mãi</font></blink>
								<? }?>
								
                            </h1> 
                            <p class="truncate" style="text-align:left;">
                                <?=$t_mota?>
                            </p>        
                            <p style="padding-bottom:5px;">                                
                                Shop: <a href="<?=$root?>/shop-<?=$t_nguoidang?>/<?=tenNguoiDung2($t_nguoidang)?>" class="popup-profile"> <?=tenNguoiDung($t_nguoidang)?></a>      
                            </p> 
							<? if($t_sapramat == 1){ ?>
								<blink><font color="#FF0000" style="font-size:11px; font-weight:bold;">Sắp ra mắt</font></blink>
							<? }?>
                            <p style="font-size:8pt;color:Gray;">

                                <?=$t_xemgannhat?> | Xem: <?=$solanxem?> | Comments: <?=$comment?>
                            </p>	
                        </td>
                        <td class="col-price">
                            <p>
                                <?=$t_giaban?>                                        
                            </p>
                        </td>
                        <td class="col-region">
                            <p>

                                <a href="javascript:void(0);"><?=$t_tinhthanh?></a>
                            </p>
                        </td>
                    </tr>  
                 <?
				 	if($i != $db->num_rows($rs)){
				 ?>   
                    <tr>
                        <td colspan="4" style="border-bottom:solid 1px #CCCCCC;height:1px;font-size:0pt;"></td>
                    </tr>                       
                 <? } ?>   
                <? } ?> 
 <? }else{ ?>
 				<tr>
					<td align="left" style="padding-left:10px;">Không có sản phẩm nào !</td>
				</tr>
 <? } ?>                   
                </table> 
                
        </div>
    </div>
    <div id="product-list-footer"></div>
</div>

<!-- Paging { -->


<div id="paging">
    <div class="corner-round-01-body" style="text-align:right">            
    <p align="center" class="font">
	 <?php echo page_div( "".$root."/?mod=chungloai&cid=".$cid."&sid=".$sid."&tukhoa=".$tukhoa."&act=".$act."&fgid=".$fgid."&tgid=".$tgid."&ttid=".$ttid."&p=%d_pg", "10", ceil ( $numofpages ), $page ); ?>
	 </p>
    </div>
</div>   

<!-- } Paging -->

<!-- } Product List -->